//
//  OrderBaseController.m
//  JiFengDaRen
//
//  Created by guest on 16/1/31.
//  Copyright © 2016年 SH_iOS. All rights reserved.
//
#import "OrderBaseController.h"
#import "OrderCell.h"
#import "OrderModel.h"
#import "StoreModel.h"
@interface OrderBaseController ()<UICollectionViewDataSource,UICollectionViewDelegate,OrderCellDelegate>

{
    UICollectionView        *_collectionView;
    UICollectionViewLayout  *_layout;
    NSMutableArray          *_orderDataArr;
    int                     _pageindex;
    int                     _count;
}

@end

@implementation OrderBaseController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self  configUI];
    _count = 5;
    _pageindex = 1;
    [self refreshFinishedOrder];


    
}
#pragma mark ----- 初始化UI
-(void)configUI{
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    _orderDataArr =  [NSMutableArray arrayWithCapacity:0];
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 10;
    CGFloat width = KScreenWidth;
    //设置滚动方向（水平、竖直）
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(width,180);
    layout.sectionInset = UIEdgeInsetsMake(5,0, 0, 0);
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,KScreenHeight - KNAVIGATIONBARHEIGHT - 44) collectionViewLayout:layout];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    _collectionView.showsVerticalScrollIndicator = NO;
    [_collectionView registerNib:[UINib nibWithNibName:@"OrderCell" bundle:nil] forCellWithReuseIdentifier:@"ordercell"];
    [self.view addSubview:_collectionView];
    [_collectionView reloadData];
    
}


#pragma mark -- 获取完成订单详情
-(void)getFinishedOrderDataIsRemove:(BOOL)isRemove  url:(NSString *)url{

    if (isRemove) {
        [_orderDataArr removeAllObjects];
    }
    
    
    NSDictionary *params ;
    if (self.index == 0) {
        params =
        @{
          @"pageindex":@(_pageindex),
          @"count":@(_count),
          @"status":@(199),
          };
        
    }else{
        
        params =
        @{
          @"pageindex":@(_pageindex),
          @"count":@(_count),
          };
    }
   
    [MyDataService requestAFURL:url   httpMethod:@"GET" params:params data:nil complection:^(id result) {
        NSLog(@"用户订单信息%@",result);
        if ([result[@"success"] isEqualToNumber:@(1)] && [result[@"data"] count] != 0) {
            for (NSDictionary *dict in result[@"data"]) {
                OrderModel *model = [[OrderModel alloc]init];
                [model setValuesForKeysWithDictionary:dict];
                [_orderDataArr addObject:model];
                
            }
               [_collectionView reloadData];
            

        }
            [_collectionView.mj_header endRefreshing];
            [_collectionView.mj_footer endRefreshing];
        } failure:^(id erro) {
              [_collectionView.mj_header endRefreshing];
              [_collectionView.mj_footer endRefreshing];

    }];
    
    
}

#pragma mark - CollectionViewDelegate
//默认为1个section
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//返回每个section中cell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{  if(_orderDataArr.count == 0){
    return 0;
 }
    return _orderDataArr.count;
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    //从重用队列中取，如果没取到会自动创建
    OrderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ordercell" forIndexPath:indexPath];
    cell.delegate = self;
    if (_orderDataArr.count) {
        OrderModel *model = _orderDataArr[indexPath.row];
        NSLog(@"数据源====%@------%@",model.shoppingcartitems,model);
        cell.backgroundColor = [UIColor whiteColor];
        if (self.index == 0) {
            if (_orderDataArr.count != 0){
                cell.model = _orderDataArr[indexPath.row];
            }
            
        }else{
            OrderModel *model = _orderDataArr[indexPath.row];
            if ([model.managment_memo isEqualToString:@"到店消费"]) {
                cell.model2 = model;
            }else{
                cell.model = model;
            }
            
        }

    }
    
    return cell;
}

//选中cell
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}
-(void)orderCell:(OrderCell *)orderCell state:(NSString *)state orderModel:(OrderModel *)orderModel{
    
    if (_jumpTopPayCente) {
        _jumpTopPayCente(state,[NSString stringWithFormat:@"%d",orderModel._id],[NSString stringWithFormat:@"%.2f",orderModel.price]);
    }
         
}


-(void)refreshFinishedOrder{
    
    // 下拉刷新
    _collectionView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _pageindex = 1;
        [self getFinishedOrderDataIsRemove:YES url:KPlace_store_order];
        
    }];
    [_collectionView.mj_header beginRefreshing];
    
    // 上拉加载更多
    _collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _pageindex ++;
        
        [self getFinishedOrderDataIsRemove:NO url:KPlace_store_order];
        
    }];
   
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
